package com.jf.net.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.text.TextUtils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;

public class AppUtil {

    private static final String TAG = "AppUtil";

    private static WeakReference<Context> sContext;

    private AppUtil(){}

    public static void init(Context context) {
        sContext = new WeakReference<>(context);
    }

    public static Context getAppContext() {
        return sContext.get();
    }

    public static String getProjectId() {
        String id = "";
        try {
            if (sContext!=null) {
                PackageManager manager = sContext.get().getPackageManager();
                ApplicationInfo appInfo = manager.getApplicationInfo(sContext.get().getPackageName(), PackageManager.GET_META_DATA);
                if (appInfo == null || appInfo.metaData == null) {
                    SELog.e(TAG, "getProjectId failed");
                    return id;
                }
                id = appInfo.metaData.getString("projectId");
                SELog.e(TAG, "getProjectId projectID:" + id);
                return id;
            }else return "";
        } catch (PackageManager.NameNotFoundException e) {
            SELog.logger(AppUtil.class.getName()).error("PackageManager.NameNotFoundException Error", e);
            return "";
        }
    }

    public static String getEnvironmentType(){
        String environmentType = "";
        try {
            PackageManager manager = sContext.get().getPackageManager();
            ApplicationInfo appInfo = manager.getApplicationInfo(sContext.get().getPackageName(), PackageManager.GET_META_DATA);
            if(appInfo == null || appInfo.metaData == null){
                SELog.e(TAG, "getEnvironmentType failed");
                return environmentType;
            }
            environmentType = appInfo.metaData.getString("environmentType");
            SELog.d(TAG, "getEnvironmentType environmentType:" + environmentType);
            return environmentType;
        } catch (PackageManager.NameNotFoundException e) {
            SELog.logger(AppUtil.class.getName()).error("PackageManager.NameNotFoundException Error", e);
            return environmentType;
        }
    }

    public static String getAppVersionName(String defaultName) {
        if(defaultName == null){
            defaultName = "";
        }
        String versionName = defaultName;
        try {
            if(getAppContext() == null){
                return defaultName;
            }
            PackageManager pm = getAppContext().getPackageManager();
            PackageInfo info = pm.getPackageInfo(getAppContext().getPackageName(),0);
            versionName = info.versionName;
        } catch (Exception e) {
            SELog.e(TAG, "Exception", e);
        }
        return TextUtils.isEmpty(versionName) ? defaultName : versionName;
    }

    public static InputStream getInputStreamOfAssets(String assetFileName) {
        SELog.d("AppUtils", "getInputStreamOfAssets assetFileName " + assetFileName);
        AssetManager assetManager = sContext.get().getApplicationContext().getAssets();
        try {
            return assetManager.open(assetFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
